package com.antgroup.seckill.business.common.config;

import com.antgroup.seckill.business.common.properties.ElasticsearchProperties;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author wangxy
 * @date 2024/8/18 10:47
 */
@Configuration
public class ElasticsearchConfiguration {

    @Value("${elasticsearch.uris}")
    private String elasticsearchUris;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;

    @Value("${elasticsearch.socket-timeout}")
    private int socketTimeout;

    @Value("${elasticsearch.connection-timeout}")
    private int connectionTimeout;

    //两种方式获取application.yml值
   @Autowired
   private ElasticsearchProperties elasticsearchProperties;

    @Bean(destroyMethod = "close", name = "esClient")
    public RestHighLevelClient restHighLevelClient() {
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        HttpHost[] hosts = Arrays.stream(elasticsearchUris.split(","))
                .map(HttpHost::create)
                .toArray(HttpHost[]::new);

        RestClientBuilder builder = RestClient.builder(hosts)
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .setDefaultIOReactorConfig(IOReactorConfig.custom()
                                .setSoTimeout(socketTimeout)
                                .build()))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout(connectionTimeout * 1000)
                        .setSocketTimeout(socketTimeout * 1000));


        return new RestHighLevelClient(builder);
    }

   /* @Bean(destroyMethod = "close", name = "esClient")
    public RestHighLevelClient restHighLevelClient() {
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticsearchProperties.getUsername(), elasticsearchProperties.getPassword()));

        HttpHost[] hosts = Arrays.stream(elasticsearchProperties.getUris().split(","))
                .map(HttpHost::create)
                .toArray(HttpHost[]::new);

        RestClientBuilder builder = RestClient.builder(hosts)
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .setDefaultIOReactorConfig(IOReactorConfig.custom()
                                .setSoTimeout(elasticsearchProperties.getSocketTimeout())
                                .build()))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout((elasticsearchProperties.getConnectionTimeout()) * 1000)
                        .setSocketTimeout((elasticsearchProperties.getSocketTimeout()) * 1000));


        return new RestHighLevelClient(builder);
    }*/
}
